package com.zsule.controllers.front;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zsule.constants.CoreConstant;
import com.zsule.controllers.BaseController;
import com.zsule.entity.EOrder;
import com.zsule.entity.EOrderDetail;
import com.zsule.entity.EUser;
import com.zsule.entity.PageEntity;
import com.zsule.service.IOrderDetailService;
import com.zsule.service.IOrderService;

@Controller
public class BillsController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(BillsController.class);
	
	@Autowired
	private IOrderService eorderService;

	@Autowired
	private IOrderDetailService eorderDetailService;
	@RequestMapping("/bills")
	public ModelAndView myBills(HttpServletRequest request,
			HttpServletResponse response, EOrder query,
			@ModelAttribute("page") PageEntity page,@ModelAttribute("searchTime") String searchTime) {
		ModelAndView mav = new ModelAndView("/euser/mybill.httl");
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		this.setPage(page);
		this.getPage().setPageSize(5);
		if (query == null
				|| (query.getUserId() == null && query.getShopperId() == null)) {
			query = new EOrder();
			query.setStatus(-1);
			query.setUserId(user.getId());
		}

		List<EOrder> orders = eorderService.getMyBillsByPage(query, page,searchTime);
		for (EOrder order : orders) {
			if (order.getStatus() == CoreConstant.EORDER_STATUS_NEW
					&& order.getCreateTime().getTime() + 600000 < new Date()
							.getTime()) {
				eorderService.changeOrderStauts(order.getId(),
						CoreConstant.EORDER_STATUS_CANCEL);
				order.setStatus(CoreConstant.EORDER_STATUS_CANCEL);
				// orders.remove(order);
			}
		}
		mav.addObject("user", user);
		mav.addObject("orders", orders);
		mav.addObject("query", query);
		mav.addObject("page", this.getPage());
		mav.addObject("searchTime", searchTime);
		mav.addObject("leaf", "userBills");
		return mav;
	}

	@RequestMapping("/shop/bills")
	public ModelAndView shopBills(HttpServletRequest request,
			HttpServletResponse response, EOrder query,
			@ModelAttribute("page") PageEntity page,@ModelAttribute("searchTime") String searchTime) {
		ModelAndView mav = new ModelAndView("/euser/shopbill.httl");
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		this.setPage(page);
		this.getPage().setPageSize(5);
		if (query == null
				|| (query.getUserId() == null && query.getShopperId() == null)) {
			query = new EOrder();
			query.setStatus(-1);
			query.setShopperId(user.getId());
		}

		List<EOrder> orders = eorderService.getMyBillsByPage(query, page,searchTime);
		for (EOrder order : orders) {
			if (order.getStatus() == CoreConstant.EORDER_STATUS_NEW
					&& order.getCreateTime().getTime() + 600000 < new Date()
							.getTime()) {
				eorderService.changeOrderStauts(order.getId(),
						CoreConstant.EORDER_STATUS_CANCEL);
				order.setStatus(CoreConstant.EORDER_STATUS_CANCEL);
				// orders.remove(order);
			}
		}
		mav.addObject("user", user);
		mav.addObject("orders", orders);
		mav.addObject("query", query);
		mav.addObject("page", this.getPage());
		mav.addObject("searchTime", searchTime);
		mav.addObject("leaf", "shopBills");
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getBillDetail", method = RequestMethod.POST)
	public EOrder getBillDetail(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorder = eorderService.findById(eorder.getId());
			List<EOrderDetail> details = eorderDetailService
					.getOrderDetail(eorder.getId());
			eorder.setDetails(details);
		} catch (Exception e) {
			logger.error("BillsController.getBillDetail", e);
		}
		return eorder;
	}
}
